import O1HD from "../../engines/gameEngine";
import { CsvFileKind } from "../../engines/libs/CsvFileKind";
import PageBase from "../Base/PageBase";
import { IPagePramsBase } from "../Interface";
import peopleArcheryRoleItem from "../Obj/peopleArcheryRoleItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class peopleArcherySelectRolePage extends PageBase {

    @property({type:cc.Node,tooltip:"角色item"})
    roleItem:cc.Node = null;

    @property({type:cc.PageView,tooltip:"角色content"})
    pageView:cc.PageView = null;

    @property({type:cc.Node,tooltip:"主动技能属性节点"})
    activeSkillNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"被动技能属性节点"})
    passiveSkillNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"左边节点"})
    leftNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"右边节点"})
    rightNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"升级按钮"})
    btnUpgrade:cc.Node = null;

    @property({type:cc.Node,tooltip:"解锁按钮"})
    btnUnlock:cc.Node = null;

    @property({type:cc.Node,tooltip:"使用按钮"})
    btnUse:cc.Node = null;

    @property({type:cc.RichText,tooltip:"主动技能文本"})
    activeSkillRichText:cc.RichText = null;

    @property({type:cc.RichText,tooltip:"被动技能文本"})
    passiveSkillRichText:cc.RichText = null;

    async OnPageOpen(prams: IPagePramsBase): Promise<void> {
        super.OnPageOpen(prams);
        this.loadPage();
    }

    loadPage(){
        let ids = O1HD.getManagers().csvDataManager.getCsvIds(CsvFileKind.CsvFile.peopleArcheryRole);
        for(let i =0;i<ids.length;i++){
            let id = ids[i];
            let data = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.peopleArcheryRole,id);
            let item = cc.instantiate(this.roleItem);
            let is = item.getComponent(peopleArcheryRoleItem);
            item.active = true;
            is.bindData(data);
            this.pageView.addPage(item);
        }
        this.refrenshPage();
    }
    
    onBtnLeft(){
        let pageIndex:number = this.pageView.getCurrentPageIndex();
        if(pageIndex > 0){
            // 可以左
            this.pageView.scrollToPage(pageIndex - 1,0.2);
        }
    }

    onBtnRight(){
        let pageIndex:number = this.pageView.getCurrentPageIndex();
        if(pageIndex < this.pageView.getPages().length - 1){
            // 可以右
            this.pageView.scrollToPage(pageIndex + 1,0.2);
        }
    }

    refrenshPage(){
        this.leftNode.active = this.pageView.getCurrentPageIndex() > 0;
        this.rightNode.active = this.pageView.getCurrentPageIndex() < this.pageView.getPages().length - 1;
        this.refenshItem();
        this.refrenshTalent();
        O1HD.emit("peopleArcheryRoleItem.refrensh");
    }

    refrenshTalent(){
        let curNode:cc.Node = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        let cs = curNode.getComponent(peopleArcheryRoleItem);
        let cData = cs.data;
        let id = cData.role_id;
        let saveData = O1HD.getManagers().peopleArcheryManager.getRoleData(id);
        // if(saveData){
            let level = saveData?.level || 1;
            let fileName:string = cData["role_file"];
            let filePath:string = `config/csv/peopleArchery/role/${fileName}`;
            let data = O1HD.getManagers().csvDataManager.getCsvDataItem(filePath,level);

            let active_Talent:string = data["active_Talent"];

            let activeTalent = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.peopleArcheryTalent,active_Talent);
            let activeFile = activeTalent["talent_file"];
            let activeData = O1HD.getManagers().csvDataManager.getCsvDataItem(`config/csv/peopleArchery/talent/${activeFile}`,level);
            let activeTypes:string[] = activeData["param_type"].split("|");
            let activeParams:string[] = activeData["param_num"].split("|");

            let active = {param_type:activeTypes[0],option_param:activeParams[0]};
            let activeOneText:string = O1HD.getManagers().peopleArcheryManager.parseDataAsOptionText(active);
            let activeTwoText:string = O1HD.getManagers().peopleArcheryManager.parseDataAsParamText(active);
            this.activeSkillRichText.string = `<color=#391102>${activeOneText}</c><color=#FF0000>${activeTwoText}</c>`;

            let passive_Talent = data["passive_Talent"];
            let passiveTalent = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.peopleArcheryTalent,passive_Talent);
            let passiveFile = passiveTalent["talent_file"];
            let passiveData = O1HD.getManagers().csvDataManager.getCsvDataItem(`config/csv/peopleArchery/talent/${passiveFile}`,level);
            let passiveTypes:string[] = passiveData["param_type"].split("|");
            let passiveParams:string[] = passiveData["param_num"].split("|");

            let passive = {param_type:passiveTypes[0],option_param:passiveParams[0]};
            let passiveOneText:string = O1HD.getManagers().peopleArcheryManager.parseDataAsOptionText(passive);
            let passiveTwoText:string = O1HD.getManagers().peopleArcheryManager.parseDataAsParamText(passive);
            this.passiveSkillRichText.string = `<color=#391102>${passiveOneText}</c><color=#FF0000>${passiveTwoText}</c>`;


        // }else{
        //     this.activeSkillRichText.string = `<color=#391102>需要解锁角色</c>`;
        //     this.passiveSkillRichText.string = `<color=#391102>需要解锁角色</c>`;
        // }
    }

    refenshItem(){
        let curNode:cc.Node = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        let cs = curNode.getComponent(peopleArcheryRoleItem);
        let cData = cs.data;
        let id = cData.role_id;
        let saveData = O1HD.getManagers().peopleArcheryManager.getRoleData(id);
        this.btnUnlock.active = saveData == null;
        this.btnUpgrade.active = saveData != null;
        this.btnUse.active = saveData != null;
        if(saveData){
            let level:number = saveData.level;
            let fileName:string = cData["role_file"];
            let filePath:string = `config/csv/peopleArchery/role/${fileName}`;
            let ids:string[] = O1HD.getManagers().csvDataManager.getCsvIds(filePath);
            let maxNode:cc.Node = this.btnUpgrade.getChildByName("maxNode");
            let upgradeMoneyNode:cc.Node = this.btnUpgrade.getChildByName("moneyNode");
            let upgradeAdNode:cc.Node = this.btnUpgrade.getChildByName("adNode");
            maxNode.active = false;
            upgradeAdNode.active = false;
            upgradeMoneyNode.active = false;
            if(level >= ids.length){
                // 等级已满
                maxNode.active = true;
            }else{
                // 获取当前等级的升级数据
                let param = O1HD.getManagers().csvDataManager.getCsvDataItem(filePath,level);
                let type = param.upgrade_type;
                let num = param.upgrade_param;
                let lbNum:cc.Label = null;
                switch(type){
                    case "0":{
                        lbNum = upgradeMoneyNode.getChildByName("layout").getChildByName("lbMoney").getComponent(cc.Label);
                        lbNum.string = `${num}`;
                        upgradeMoneyNode.active = true;
                        break;
                    }
                    case "1":{
                        lbNum = upgradeAdNode.getChildByName("layout").getChildByName("lbAd").getComponent(cc.Label);
                        lbNum.string = "升级";
                        upgradeAdNode.active = true;
                        break;
                    }
                }
            }
        }else{
            let unlockType:string = cData["role_unlock_type"];
            let unlockParam:number = cData["role_unlock_param"];
            let unlockMoneyNode:cc.Node = this.btnUnlock.getChildByName("moneyNode");
            let unlockAdNode:cc.Node = this.btnUnlock.getChildByName("adNode");
            let lbUnlockMoney:cc.Label = unlockMoneyNode.getChildByName("layout").getChildByName("lbMoney").getComponent(cc.Label);
            let lbAdUnlock:cc.Label = unlockAdNode.getChildByName("layout").getChildByName("lbAd").getComponent(cc.Label);
            unlockAdNode.active = false;
            unlockMoneyNode.active = false;
            switch(unlockType){
                case "1":{
                    lbUnlockMoney.string = `${unlockParam}`;
                    unlockMoneyNode.active = true;
                    break;
                }
                case "2":{
                    lbAdUnlock.string = `${O1HD.getManagers().peopleArcheryManager.getData().adNum[cData["role_id"]] || 0}/${unlockParam}`;
                    unlockAdNode.active = true;
                    break;
                }
            }
        }
        let useLb:cc.Label = this.btnUse.getChildByName("lbUse").getComponent(cc.Label);
        useLb.string = cData.role_id == O1HD.getManagers().peopleArcheryManager.getData().curUseRoleId ? "已出战" : "出战";
    }

    onBtnUpGrade(){
        // 升级
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        let curNode:cc.Node = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        let cs = curNode.getComponent(peopleArcheryRoleItem);
        let cData = cs.data;
        let id = cData.role_id;
        let saveData = O1HD.getManagers().peopleArcheryManager.getRoleData(id);
        if(!saveData){
            O1HD.getModules().uiControlModule.showMessageBox("请先解锁");
            return;
        }
        let fileName:string = cData["role_file"];
        let filePath:string = `config/csv/peopleArchery/role/${fileName}`;
        let level:number = saveData.level;
        let ids:string[] = O1HD.getManagers().csvDataManager.getCsvIds(filePath);
        if(level >= ids.length){
            // 等级已满
            O1HD.getModules().uiControlModule.showMessageBox("等级已满");
            return;
        }
        let d = O1HD.getManagers().csvDataManager.getCsvDataItem(filePath,saveData.level);
        let upgrade:string = d["upgrade_type"];
        switch(upgrade){
            case "0":{
                let money = Number(d["upgrade_param"]);
                if(O1HD.getManagers().peopleArcheryManager.getData().money >= money){
                    O1HD.getManagers().peopleArcheryManager.setData({money:-money},true);
                    this.onUpGrade();
                }else{
                    O1HD.getModules().uiControlModule.showMessageBox("修为不够");
                }
                break;
            }
            case "1":{
                if(O1HD.getServices().userSrv.getCoins() > 0){
                    this.onUpGrade();
                    O1HD.getServices().userSrv.setCoins(-1,true);
                }else{
                    O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                        this.onUpGrade();
                    },()=>{

                    },null,"箭箭箭看广告升级人物");
                }
                break;
            }
        }
    }

    onBtnUnlock(){
        // 解锁
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        let curNode:cc.Node = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        let cs = curNode.getComponent(peopleArcheryRoleItem);
        let cData = cs.data;
        let id = cData.role_id;
        let saveData = O1HD.getManagers().peopleArcheryManager.getRoleData(id);
        if(saveData){
            O1HD.getModules().uiControlModule.showMessageBox("已经解锁");
            return;
        }
        let type = cData["role_unlock_type"];
        let param:number = Number(cData["role_unlock_param"]);
        switch(type){
            case "1":{
                if(O1HD.getManagers().peopleArcheryManager.getData().money >= param){
                    O1HD.getManagers().peopleArcheryManager.setData({money:-param},true);
                    this.onUnlock();
                }else{
                    O1HD.getModules().uiControlModule.showMessageBox("钱不够");
                }
                break;
            }
            case "2":{
                if(O1HD.getServices().userSrv.getCoins() > 0){
                    let videoCount:number = O1HD.getManagers().peopleArcheryManager.getData().adNum[id];
                    if(videoCount == null){
                        O1HD.getManagers().peopleArcheryManager.getData().adNum[id] = 0;
                        videoCount = 0;
                    }
                    videoCount ++;
                    O1HD.getManagers().peopleArcheryManager.getData().adNum[id] = videoCount;
                    O1HD.getManagers().peopleArcheryManager.writeData();
                    if(videoCount >= param){
                        this.onUnlock();
                    }
                    O1HD.getServices().userSrv.setCoins(-1,true);
                    this.refenshItem();
                }else{
                    O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                        let videoCount:number = O1HD.getManagers().peopleArcheryManager.getData().adNum[id];
                        if(videoCount == null){
                            O1HD.getManagers().peopleArcheryManager.getData().adNum[id] = 0;
                            videoCount = 0;
                        }
                        videoCount ++;
                        O1HD.getManagers().peopleArcheryManager.getData().adNum[id] = videoCount;
                        O1HD.getManagers().peopleArcheryManager.writeData();
                        if(videoCount >= param){
                            this.onUnlock();
                        }
                        this.refenshItem();
                    },()=>{

                    },null,"箭箭箭看广告解锁人物");
                }
                break;
            }
        }
    }

    onBtnUse(){
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        this.onUse();
    }

    onUse(){
        let curNode:cc.Node = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        let cs = curNode.getComponent(peopleArcheryRoleItem);
        let cData = cs.data;
        let id = cData.role_id;
        O1HD.getManagers().peopleArcheryManager.setData({curUseRoleId:id},false);
        this.refrenshPage();
        O1HD.emit("peopleArcheryStartPage.refrenshRole");
    }

    onUpGrade(){
        let curNode:cc.Node = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        let cs = curNode.getComponent(peopleArcheryRoleItem);
        let cData = cs.data;
        let id = cData.role_id;
        let saveData = O1HD.getManagers().peopleArcheryManager.getRoleData(id);
        saveData.level++;
        this.refrenshPage();
        O1HD.getManagers().peopleArcheryManager.writeData();
    }

    onUnlock(){        
        let curNode:cc.Node = this.pageView.getPages()[this.pageView.getCurrentPageIndex()];
        let cs = curNode.getComponent(peopleArcheryRoleItem);
        let cData = cs.data;
        let id = cData.role_id;
        O1HD.getManagers().peopleArcheryManager.addOneRole(id);
        this.refrenshPage();
    }

}
